<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h1>注册</h1>
    <input type="text" id="username">
    <input type="text" id="password">
    <input type="button" value="注册" id="registerBtn">

    <script src="./js/jquery.min.js"></script>

    <script>
        var bool = false
        $("#username").blur(function () {
            const username = $("#username").val();
            $.ajax({
                url: '/users/verify',
                type: 'POST',
                data: {
                    username
                },
                success(msg) {
                    if (msg) {
                        alert("该账号已注册！")
                        bool = false
                    } else {
                        bool = true
                    }
                }
            })
        })
        $("#registerBtn").click(function () {
            const username = $("#username").val();
            const password = $("#password").val();
            if (bool == true) {
                $.ajax({
                    url: '/users/register',
                    type: 'POST',
                    data: {
                        username,
                        password
                    },
                    success(msg) {
                        if (msg) {
                            alert("注册成功！")
                            location.href = "./login.html";
                        }
                    }
                })
            }

        })
    </script>
</body>

</html>